using System;
using Sce.PlayStation.Core;
using Sce.PlayStation.Core.Input;
using Nyanimals_UI;
using Sce.PlayStation.HighLevel.UI;
namespace Nyanimals
{
	public class CWorldState : CBaseState
	{
		CPlayer Player;
        
		NyanimalDisplay display;
        CEventListener listener;
        
		public CWorldState ()
		{
		}
        
        private void HandleEvent(CBaseObject obj, CBaseEvent e)
        {
            switch(e.ID)
            {
            case EventID.PICKUPOBJECT_EVENT:
                CPickupObjectEvent po = (CPickupObjectEvent)e;
                display.SetObjectInformation(po.HeldObject);
                
                if(po.HeldObject == null)
                {
                    UISystem.PopScene();
                }
                else
                {
                    UISystem.PushScene(display);
                }
                break;
            }
        }
		//CollisionBox box;
		public void Enter()
		{
			CObjectManager.Instance.CreatePlayer("Charactors/Dot/SR_Char_Dot.SRM", "Charactors/Dot/SR_Char_Dot.SRA", 10.0f, Matrix4.Identity, out Player);
			
            // create a food box
            CObjectManager.Instance.AddFoodBin(Matrix4.Translation(new Vector3(0.0f, 0.0f, 00.0f)));
            
            //box = new CollisionBox(new Vector3(0, 1, 0), 1, 1, 1);
            
            // setup the display
            display = new NyanimalDisplay();
            UISystem.ScenePushTransition = new CrossFadeTransition();
            UISystem.ScenePopTransition = new CrossFadeTransition();
            
            listener = new CEventListener(null, HandleEvent);
            CEventManager.Instance.RegisterClient(ref listener);
            
            // place Nyanimals
            int count = 0;
            int spread = 50 * count;
            for(int i = 0; i < count; ++i)
            {
                Vector3 pos = new Vector3();
                pos.X = (float)CRandom.Next(-spread, spread) / 100.0f;
                pos.Y = 0.0f;
                pos.Z = (float)CRandom.Next(-spread, spread) / 100.0f;
                CNyanimal nyan = CObjectManager.Instance.CreateNyanimal(Matrix4.Translation(pos));
                
                nyan.Hunger = (float)CRandom.Next(0, 100);
                nyan.Name = "Nyanimal #" + (i + 1).ToString();
            }
            
            // place food
            count *= 0;
            spread = spread * 10;
            for(int i = 0; i < count; ++i)
            {
                Vector3 pos = new Vector3();
                pos.X = (float)CRandom.Next(-spread, spread) / 100.0f;
                pos.Y = 0.0f;
                pos.Z = (float)CRandom.Next(-spread, spread) / 100.0f;
                CObjectManager.Instance.CreateFood(FoodTypes.NyanBerry_Food, Matrix4.Translation(pos));
            }
                       
						
		}
		
		public void Exit()
		{
            UISystem.SetScene(null);
            CEventManager.Instance.UnregisterClient(ref listener);
		}
		
		public void Input()
		{
			CObjectManager.Instance.Input();
			
			
            // for debugging now
            if(CInput.Instance.ButtonPressed(GamePadButtons.Triangle))
            {
                // place a NyanBerry
                Matrix4 mat = Matrix4.Identity;
                mat.AxisW = Player.Position + (Player.LocalMatrix.AxisZ * 1.0f);
                
                CObjectManager.Instance.CreateFood(FoodTypes.NyanBerry_Food, mat);                    
            }
           
            if(CInput.Instance.ButtonPressed(GamePadButtons.R))
            {
                Console.WriteLine("Memory in use in bytes: " + GC.GetTotalMemory(true) + " F");                  
            }
            
            
		}
		
		public void Update(float deltaTime)
		{
			CObjectManager.Instance.Update(deltaTime);
            CObjectManager.Instance.UpdateCamAroundObject(Player, deltaTime);
			            
            // end of frame stuff
            
		}
		
		public void Render()
		{
		}
		
		public StateTypes GetStateType()
		{
			return StateTypes.Game_State;
		}
	}
}

